<template>
  <div>
    <el-dialog title="专区banner管理" :visible="visible" width="1200px" :destroy-on-close="true" :lock-scroll="false"
      @update:visible="updateVisible">
      <!-- 搜索表单 -->
      <el-form :model="where" label-width="77px" class="ele-form-search" @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">

          <el-col :lg="6" :md="12">
            <el-form-item label="广告名称:">
              <el-input :placeholder="'请输入广告名称'" v-model="where.name"></el-input>
            </el-form-item>
          </el-col>

          <el-col :lg="8" :md="12">
            <el-form-item label="创建时间:">
              <el-date-picker style="width: 100%" v-model="where.time_range" type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss" label-format="yyyy-MM-dd HH:mm:ss" range-separator="至"
                start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="8" :md="12">
            <div class="ele-form-actions">
              <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openEdit(null)"
            v-if="permission.includes('sys:gamebanner:add')">添加
          </el-button>
        </template>

        <!-- <template slot="device" slot-scope="{row}">
          <el-tag v-if="row.device === 1" type="primary" size="small">全部</el-tag>
          <el-tag v-if="row.device === 2" type="success" size="small">Android</el-tag>
          <el-tag v-if="row.device === 3" type="danger" size="small">ios</el-tag>
        </template> -->

        <template slot="device" slot-scope="{ row }">
          <span v-for="(value, index) in row.device.split(',')" :key="index" style="padding-left: 8px;">
            <el-tag size="mini" v-if="value == ''">暂无</el-tag>
            <el-tag size="mini" v-else :type="['success', 'primary', 'warning', 'info', 'info', 'danger'][index - 1]">{{
              ['全部', '安卓', 'IOS', 'PAD', 'IPAD', 'PC'][value-1] }}</el-tag>
          </span>
        </template>

        <template slot="banner" slot-scope="{ row }">
          <el-image style="width: 30px; height: 30px" :src="row.banner" fit="fit"></el-image>
        </template>

        <template slot="action" slot-scope="{ row }">
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)"
            v-if="permission.includes('sys:gamebanner:edit')">编辑
          </el-link>
        </template>

        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch v-model="row.status" @change="status(row)" :active-value="1" :inactive-value="0" />
        </template>

      </ele-pro-table>
    </el-dialog>
    <!-- 编辑弹窗 -->
    <Edit :data="current" :visible.sync="showEdit" @done="reload" v-if="showEdit" />
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import Edit from "./edit";

export default {
  name: "BoxGame",
  components: { Edit },
  computed: {
    ...mapGetters(["permission"])
  },
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表格数据接口
      url: "/BoxGameBanner/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 45,
          align: "center",
          fixed: "left"
        },

        {
          prop: "id",
          label: "ID",
          width: 100,
          align: "center",
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: "name",
          label: "广告名称",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          columnKey: "banner",
          prop: "banner",
          label: "banner图片",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center",
          slot: "banner"
        },
        {
          prop: "url",
          label: "URL",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "device",
          label: "投放设备",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center",
          slot: "device"
        },
        {
          prop: 'status',
          label: '是否启用',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          slot: 'status',
        },

        {
          prop: "publish_start_time",
          label: "展示时间",
          showOverflowTooltip: true,
          align: "center",
          minWidth: 300,
          formatter: (row, column, cellValue) => {
            const startTime = this.$util.toDateString(cellValue) + " ~\n";
            const endTime = this.$util.toDateString(row.publish_end_time);
            return `${startTime}${endTime}`;
          }
        },
        {
          prop: "sort",
          label: "排序",
          showOverflowTooltip: true,
          align: "center"
        },
        {
          prop: "desc",
          label: "备注",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "create_time",
          label: "创建时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: "action",
          label: "操作",
          width: 150,
          align: "center",
          resizable: false,
          slot: "action",
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: Object.assign({}, { game_id: this.data.id }),
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 当前行id数据
      gameRowID: null,
      // 是否显示编辑弹窗
      showEdit: false
    };
  },
  watch: {
    visible() {
      this.where = { game_id: this.data.id };
    },
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = { game_id: this.data.id };
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      row = Object.assign({ game_id: this.data.id }, row),
        this.current = row;
      this.showEdit = true;
    },

    /* 更改状态 */
    status(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        "id": row.id,
        "status": row.status
      })
      this.$http.post('BoxGameBanner/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.status = !row.status ? 1 : 0;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },

    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    }
  }
};
</script>

<style scoped></style>
